/****************************************************************************
 * Copyright (C) 2013
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * MiiWindow.cpp
 *
 * for SaveGame Manager GX 2013
 ***************************************************************************/
#include "MiiWindow.hpp"
#include "../../Themes/CTheme.h"
#include "../../Controls/Application.h"

MiiWindow::MiiWindow()
	: ManageWindow()
{
	bIsEmuMii = (Settings.LoadedBrowser == EMII_BROWSER);
	
	buttons = NULL;
	ebuttons = NULL;
	
	miiInfos = new MiiInfos();
	miiInfos->SetAlignment(ALIGN_CENTER | ALIGN_TOP);
	miiInfos->SetPosition(0, 65);
	Append(miiInfos);
	
	if(!bIsEmuMii)
	{
		buttons = new MiiButtons();
		buttons->BlockButtons.connect(this, &MiiWindow::OnBlockButtons);
		buttons->EntryDeleted.connect(this, &MiiWindow::OnEntryDeleted);
		if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
			Append(buttons);
	}
	else
	{
		ebuttons = new EMiiButtons();
		ebuttons->BlockButtons.connect(this, &MiiWindow::OnBlockButtons);
		ebuttons->EntryDeleted.connect(this, &MiiWindow::OnEntryDeleted);
		if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_MANAGE))
			Append(ebuttons);
	}
	
	SetInfos();
	
	Refresh.connect(this, &MiiWindow::OnRefresh);
	
	Application::Instance()->Append(this);
	Application::Instance()->SetUpdateOnly(this);
}

MiiWindow::~MiiWindow()
{
	RemoveAll();
	
	if(buttons)
		delete buttons;
	if(ebuttons)
		delete ebuttons;

	delete miiInfos;
}

void MiiWindow::SetInfos()
{
	miiInfos->SetInfos(selIndex);
}

void MiiWindow::OnRefresh(bool inc)
{
	ManageWindow::OnRefresh(inc);
	
	if(!closing)
		SetInfos();
}

void MiiWindow::OnEntryDeleted()
{
	Explorer::Instance()->Reload();
	CurrentBrowser()->FilterList();
	
	selIndex--;
	POINT p = {0, 0};
	OnArrowButtonClick(btnRight, 0, p);
}

void MiiWindow::OnBlockButtons(bool block)
{
	ManageWindow::OnBlockButtons(block);
}
